﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace SystemHYS.Infrstructre
{
    public class Repository<T> : IRepository<T> where T :class,new() 
    {
        public SystemCoretext db;
        public Repository(SystemCoretext db)
        {
            this.db = db;
        }

        public async Task<int> AddAsyn(T entity)
        {
            await db.Set<T>().AddAsync(entity);
            return await db.SaveChangesAsync();
        }

        public async Task<int> DeleteAsyn(T entity)
        {
            db.Set<T>().Remove(entity);
            return await db.SaveChangesAsync();
        }

        public async Task<List<T>> GetAllAsnc()
        {
            return await db.Set<T>().ToListAsync();
        }

        public async Task<List<T>> GetAsnc(Expression<Func<T, bool>> predicate)
        {
            return await db.Set<T>().Where(predicate).ToListAsync();
        }

        public async Task<T> GetModelAsnc(int id)
        {
            return await db.Set<T>().FindAsync(id);
        }

        public async Task<int> UpdateAsyn(T entity)
        {
            db.Set<T>().Update(entity);
            return await db.SaveChangesAsync();
        }
    }
}
